<?php


namespace backend\models\c2\entity;


use common\models\c2\entity\Product;
use common\models\c2\entity\ProductCombination;
use common\models\c2\entity\ProductionCommitNoteItem;
use common\models\c2\entity\ProductionScheduleItem;
use common\models\c2\statics\ProductionCommitNoteState;
use Yii;
use yii\helpers\ArrayHelper;
use yii\validators\RequiredValidator;

class ProcessCommitNote extends \common\models\c2\entity\ProductionCommitNote
{

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        if (!empty($this->items)) {
            foreach ($this->items as $item) {
                $product = Product::findOne($item['enter_product_id']);
                if (!is_null($product)) {
                    $attributes = [
                        'schedule_id' => $this->schedule_id,
                        'product_id' => $product->id,
                        'product_name' => $product->name,
                        'product_sku' => $product->sku,
                        'product_label' => $product->label,
                        'product_value' => $product->value,
                        // 'combination_id' => 0,
                        // 'combination_name' => '',
                        // 'enter_product_id' => isset($item['enter_product_id']) ? $item['enter_product_id'] : 0,
                        'measure_id' => isset($item['measure_id']) ? $item['measure_id'] : 0,
                        'commit_sum' => isset($item['commit_sum']) ? $item['commit_sum'] : 0,
                        'memo' => isset($item['memo']) ? $item['memo'] : '',
                        'position' => isset($item['position']) ? $item['position'] : 0,
                    ];
                    if (isset($item['id']) && $item['id'] == '') {
                        $itemModel = new ProductionCommitNoteItem();
                        $itemModel->setAttributes($attributes);
                        $itemModel->link('owner', $this);
                    } elseif (isset($item['id'])) {
                        $itemModel = ProductionCommitNoteItem::findOne($item['id']);
                        if (!is_null($itemModel)) {
                            $itemModel->updateAttributes($attributes);
                        }
                    }
                }
            }
        }
    }

    public function setStateToCommit()
    {
        $ts = Yii::$app->db->beginTransaction();
        $items = $this->getNoteItems()->all();
        if (!empty($items)) {
            foreach ($items as $item) {
                $product = $item->product;
                $product->stock += $item->commit_sum;
                $product->save();

                $scheduleItem = ProductionScheduleItem::find()
                    ->where(['schedule_id' => $item->schedule_id, 'product_id' => $item->product_id])->one();
                $scheduleItem->enter_sum += $item->commit_sum;
                $scheduleItem->save();
            }
        }
        $ts->commit();
        $this->updateAttributes(['state' => ProductionCommitNoteState::COMMIT]);
    }

    public function getEnterProductHashMap($cond)
    {
        $data = ['' => Yii::t("app.c2", "Select options ..")];
        $items = \common\models\c2\entity\ProductionScheduleItem::find()
            ->where($cond)
            ->with('enterProduct')
            ->all();
        foreach ($items as $item) {
            $data += [
                $item->enterProduct->id => $item->enterProduct->name,
            ];
        }
        return $data;
    }

}